package com.hj.controller;

import com.hj.model.AdvanceResponse;
import com.hj.model.CartVo;
import com.hj.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author haoshen
 * @version 1.0
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 由用户id 得到他购物车中的全部商品
     *
     * @param id 用户id
     * @return 购物车中的全部商品
     */
    @GetMapping("/cartPro")
    public AdvanceResponse<CartVo> getCartProduct(HttpServletRequest request) {
        return cartService.getCartProduct(request);
    }

    /**
     * 添加商品到购物车
     */
    @GetMapping("/addPro")
    public AdvanceResponse<Object> addCartPro(Integer proId, @RequestParam(defaultValue = "1") Integer count, HttpServletRequest request) {
        return cartService.addCartPro(proId, count, request.getSession());
    }

    /**
     * 删除购物车商品
     */
    @GetMapping("/delPro")
    public AdvanceResponse<Object> delCartPro(Integer proId, Integer count, HttpServletRequest request) {
        return cartService.delCartPro(proId, count, request);
    }

    /**
     * 从购物车中减去商品数量
     */
    @GetMapping("/subPro")
    public AdvanceResponse<Object> subCartPro(Integer proId, @RequestParam(defaultValue = "1") Integer count, HttpServletRequest request) {
        return cartService.subCartPro(proId, count, request);
    }

    /**
     * 改变购物车中商品选中状态
     */
    @GetMapping("/reChecked")
    public AdvanceResponse<Object> reChecked(Integer proId, Integer checked, HttpServletRequest request) {
        return cartService.reChecked(proId, checked, request);
    }
}
